# 故障恢复系统配置文件
# 定义服务检查、恢复策略和告警配置

# 全局配置
global:
  # 检查间隔（秒）
  check_interval: {{ failover_check_interval | default(30) }}
  # 日志级别
  log_level: {{ failover_log_level | default('INFO') }}
  # 日志文件路径
  log_file: "{{ failover_log_file | default('/var/log/failover/health-check.log') }}"
  # 状态文件路径
  state_file: "{{ failover_data_dir | default('/var/log/failover') }}/service_state.json"
  # 恢复历史文件
  history_file: "{{ failover_history_file | default('/var/log/failover/recovery_history.json') }}"

# 服务检查配置
services:
{% for service in failover_services | default([]) %}
  - name: "{{ service.name }}"
{% if service.health_check is defined %}
    type: "{{ service.health_check.type | default('http') }}"
{% if service.health_check.type == 'http' or service.health_check.type is not defined %}
    url: "{{ service.health_check.url }}"
    method: "{{ service.health_check.method | default('GET') }}"
    timeout: {{ service.health_check.timeout | default(10) }}
    expected_status: {{ service.health_check.expected_status | default(200) }}
    {% if service.health_check.expected_content is defined %}
    expected_content: "{{ service.health_check.expected_content }}"
    {% endif %}
    {% elif service.health_check.type == 'tcp' %}
    host: "{{ service.health_check.host }}"
    port: {{ service.health_check.port }}
    timeout: {{ service.health_check.timeout | default(5) }}
    {% elif service.health_check.type == 'process' %}
    process_name: "{{ service.health_check.process_name }}"
    {% elif service.health_check.type == 'systemd' %}
    service_name: "{{ service.service_name }}"
    {% endif %}
{% else %}
    type: "{{ service.type | default('systemd') }}"
{% set service_type = service.type | default('systemd') %}
{% if service_type == 'http' %}
    url: "{{ service.url | default('http://localhost') }}"
    method: "{{ service.method | default('GET') }}"
    timeout: {{ service.timeout | default(10) }}
    expected_status: {{ service.expected_status | default(200) }}
    {% if service.expected_content is defined %}
    expected_content: "{{ service.expected_content }}"
    {% endif %}
    {% elif service_type == 'tcp' %}
    host: "{{ service.host | default('localhost') }}"
    port: {{ service.port | default(80) }}
    timeout: {{ service.timeout | default(5) }}
    {% elif service_type == 'process' %}
    process_name: "{{ service.process_name | default(service.name) }}"
    {% else %}
    service_name: "{{ service.service_name | default(service.name) }}"
    {% endif %}
{% endif %}
    # 恢复配置
    recovery:
      enabled: {{ service.recovery.enabled | default(true) }}
      max_attempts: {{ service.recovery.max_attempts | default(3) }}
      retry_interval: {{ service.recovery.retry_interval | default(60) }}
      restart_command: "{{ service.recovery.restart_command | default('systemctl restart ' + service.name) }}"
      {% if service.recovery.dependencies is defined %}
      dependencies:
      {% for dep in service.recovery.dependencies %}
        - "{{ dep }}"
      {% endfor %}
      {% endif %}
    # 告警配置
    alerts:
      enabled: {{ service.alerts.enabled | default(true) }}
      {% if service.alerts.webhook is defined %}
      webhook: "{{ service.alerts.webhook }}"
      {% endif %}
      {% if service.alerts.email is defined %}
      email: "{{ service.alerts.email }}"
      {% endif %}

{% endfor %}

# 系统资源检查
system_checks:
  # CPU使用率检查
  cpu:
    enabled: {{ failover_cpu_check_enabled | default(true) }}
    threshold: {{ failover_cpu_threshold | default(80) }}  # 百分比
    duration: {{ failover_cpu_duration | default(300) }}  # 持续时间（秒）
  
  # 内存使用率检查
  memory:
    enabled: {{ failover_memory_check_enabled | default(true) }}
    threshold: {{ failover_memory_threshold | default(85) }}  # 百分比
    duration: {{ failover_memory_duration | default(300) }}  # 持续时间（秒）
  
  # 磁盘使用率检查
  disk:
    enabled: {{ failover_disk_check_enabled | default(true) }}
    threshold: {{ failover_disk_threshold | default(90) }}  # 百分比
    paths:
{% for path in failover_disk_paths | default(['/']) %}
      - "{{ path }}"
{% endfor %}
  
  # 系统负载检查
  load:
    enabled: {{ failover_load_check_enabled | default(true) }}
    threshold: {{ failover_load_threshold | default(5.0) }}  # 1分钟平均负载
    duration: {{ failover_load_duration | default(300) }}  # 持续时间（秒）

# 告警配置
alerts:
  # 全局告警开关
  enabled: {{ failover_alerts_enabled | default(true) }}
  
  # Webhook告警
  webhook:
    enabled: {{ failover_webhook_enabled | default(false) }}
{% if failover_alert_webhook is defined %}
    url: "{{ failover_alert_webhook }}"
{% endif %}
    timeout: {{ failover_webhook_timeout | default(10) }}
    retry_attempts: {{ failover_webhook_retry | default(3) }}
  
  # 邮件告警
  email:
    enabled: {{ failover_email_enabled | default(false) }}
    {% if failover_smtp_server is defined %}
    smtp_server: "{{ failover_smtp_server }}"
    smtp_port: {{ failover_smtp_port | default(587) }}
    username: "{{ failover_smtp_username }}"
    password: "{{ failover_smtp_password }}"
    from_email: "{{ failover_from_email }}"
    to_emails:
    {% for email in failover_to_emails | default([]) %}
      - "{{ email }}"
    {% endfor %}
    {% endif %}
  
  # 日志告警
  log:
    enabled: {{ failover_log_alerts_enabled | default(true) }}
    file: "{{ failover_alert_log_file | default('/var/log/failover/alerts.log') }}"

# 恢复策略
recovery:
  # 全局恢复开关
  enabled: {{ failover_recovery_enabled | default(true) }}
  
  # 恢复策略
  strategy: "{{ failover_recovery_strategy | default('progressive') }}"  # progressive, immediate, manual
  
  # 渐进式恢复配置
  progressive:
    # 第一阶段：服务重启
    restart:
      max_attempts: {{ failover_restart_attempts | default(3) }}
      interval: {{ failover_restart_interval | default(60) }}
    
    # 第二阶段：升级恢复（重新部署等）
    escalation:
      enabled: {{ failover_escalation_enabled | default(false) }}
{% if failover_escalation_command is defined %}
      command: "{{ failover_escalation_command }}"
{% endif %}
      timeout: {{ failover_escalation_timeout | default(300) }}
  
  # 恢复冷却时间（避免频繁重启）
  cooldown: {{ failover_recovery_cooldown | default(300) }}  # 秒
  
  # 恢复历史保留天数
  history_retention: {{ failover_history_retention | default(30) }}  # 天

# 自定义检查脚本
custom_checks:
{% for check in failover_custom_checks | default([]) %}
  - name: "{{ check.name }}"
    script: "{{ check.script }}"
    timeout: {{ check.timeout | default(30) }}
    interval: {{ check.interval | default(300) }}
    enabled: {{ check.enabled | default(true) }}
{% endfor %}